//! 简化版本的RustCloud构建器
//! 
//! 提供基本的构建功能，不依赖外部crate

use crate::config::RustCloudConfig;
use rustcloud_core::ServiceResult;

/// 简化版本的RustCloud构建器
#[derive(Debug, Clone)]
pub struct SimpleRustCloudBuilder {
    config: RustCloudConfig,
}

impl SimpleRustCloudBuilder {
    /// 创建新的构建器实例
    pub fn new(service_name: &str) -> Self {
        let mut config = RustCloudConfig::default();
        config.service.name = service_name.to_string();
        
        Self { config }
    }

    /// 从配置文件创建构建器
    pub async fn from_config_file(path: &str) -> ServiceResult<Self> {
        let config = RustCloudConfig::from_file(path).await?;
        Ok(Self { config })
    }

    /// 从配置对象创建构建器
    pub fn from_config(config: RustCloudConfig) -> Self {
        Self { config }
    }

    /// 设置端口
    pub fn with_port(mut self, port: u16) -> Self {
        self.config.network.port = port;
        self
    }

    /// 设置服务版本
    pub fn with_version(mut self, version: &str) -> Self {
        self.config.service.version = version.to_string();
        self
    }

    /// 设置环境
    pub fn with_environment(mut self, env: &str) -> Self {
        self.config.service.environment = env.to_string();
        self
    }
    
    /// 🆕 新增：启用配置广播功能
    pub fn enable_config_broadcast(mut self) -> Self {
        self.config.components.config_broadcast.enabled = true;
        self
    }
    
    /// 🆕 新增：设置配置广播消息总线类型
    pub fn with_config_broadcast_bus(mut self, bus_type: &str) -> Self {
        self.config.components.config_broadcast.enabled = true;
        self.config.components.config_broadcast.message_bus_type = bus_type.to_string();
        self
    }
    
    /// 🆕 新增：设置配置广播主题前缀
    pub fn with_config_broadcast_topic_prefix(mut self, prefix: &str) -> Self {
        self.config.components.config_broadcast.topic_prefix = prefix.to_string();
        self
    }
    
    /// 🆕 新增：设置关注的应用列表
    pub fn with_interested_applications(mut self, applications: Vec<String>) -> Self {
        self.config.components.config_broadcast.interested_applications = applications;
        self
    }
    
    /// 🆕 新增：启用自动重启
    pub fn enable_auto_restart(mut self) -> Self {
        self.config.components.config_broadcast.auto_restart.enabled = true;
        self
    }

    /// 获取配置
    pub fn get_config(&self) -> &RustCloudConfig {
        &self.config
    }

    /// 构建配置（临时实现）
    pub async fn build(&self) -> ServiceResult<RustCloudConfig> {
        self.config.validate()?;
        Ok(self.config.clone())
    }
}

impl Default for SimpleRustCloudBuilder {
    fn default() -> Self {
        Self::new("default-service")
    }
}